package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.edu.dao.BaseDao;
import com.edu.dao.GoodDao;
import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.Tool;
import com.edu.entity.User;

public class GoodDaoImpl implements GoodDao {
	BaseDao bd1 = new BaseDao();
	@Override
	public ArrayList<Goods> sqlFindGoods(Goods good, Conditions con) {
		// TODO Auto-generated method stub
		Tool t1 = new Tool();
		ArrayList<Goods> list = new ArrayList();
		Connection conn = bd1.getConn();
		StringBuffer sb = new StringBuffer();
		ArrayList<Object> objList = new ArrayList();
		if(good.getGoodname() == null) {
			objList.add("%%");
		}else {
			objList.add("%"+good.getGoodname()+"%");
		}
		sb.append("SELECT * FROM goods WHERE goodname LIKE ?");
		int dataNum = 1;
		if(good.getGoodtype() != 0) {
			sb.append(" AND goodtype = ?");
			objList.add(good.getGoodtype());
		}
		if(good.getGoodnum() != 0) {
			sb.append(" AND goodnum = ?");
			objList.add(good.getGoodnum());
		}
		if(good.getGooddec() != null) {
			sb.append(" AND gooddec LIKE ?");
			objList.add("%"+good.getGooddec()+"%");
		}
		if(good.getGoodstat() != 0) {
			sb.append(" AND goodstat = ?");
			objList.add(good.getGoodstat());
		}
		if(good.getGoodFreightType() != 0) {
			sb.append(" AND goodFreighttype = ?");
			objList.add(good.getGoodFreightType());
		}
		if(good.getGoodTradetType() != 0) {
			sb.append(" AND goodTradetype = ?");
			objList.add(good.getGoodTradetType());
			System.out.println(good.getGoodTradetType());
		}
		if(good.getSellerId() != 0) {
			sb.append(" AND sellerid = ?");
			objList.add(good.getSellerId());
		}
		if(good.getGoodid() != 0) {
			sb.append(" AND goodid = ?");
			objList.add(good.getGoodid());
		}
		if(con != null) {
			if(con.getLowPrice() != -1 ) {
				sb.append(" AND goodprice BETWEEN ?");
				objList.add(con.getLowPrice());
			}
			if(con.getHighPrice() != -1) {
				sb.append(" AND ?");
				objList.add(con.getHighPrice());
			}
			if(con.getOrderType() != 0) {
				sb.append(t1.orderTypeToSql(con.getOrderType()));
			}
			if(con.getPageNum() != 0) {
				sb.append(" LIMIT ?");
				objList.add((con.getPageNum() - 1)*con.getPageSize());
			}
			if(con.getPageSize() != 0) {
				sb.append(" ,?");
				objList.add(con.getPageSize());
			}
		}
		String sql = sb.toString();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			for(int i = 0;i<objList.size();i++) {
				ps.setObject(i+1, objList.get(i));
			}
			rs = ps.executeQuery();
			while(rs.next()) {
				Goods g = new Goods();
				g.setGoodid(rs.getInt(1));
				g.setGoodname(rs.getString(2));
				g.setGoodprice(rs.getDouble(3));
				g.setOriprive(rs.getDouble(4));
				g.setGoodtype(rs.getInt(5));
				g.setGoodnum(rs.getInt(6));
				g.setGooddec(rs.getString(7));
				g.setGoodimg(rs.getString(8));
				g.setGoodstat(rs.getInt(9));
				g.setGoodFreightType(rs.getInt(10));
				g.setGoodTradetType(rs.getInt(11));
				g.setSellerId(rs.getInt(12));
				g.setGoodOnSellStat(rs.getInt(13));
				g.setBrand(rs.getString(14));
				list.add(g);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		
		return list;
	}
	@Override
	public User sqlFindSeller(int id) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM user WHERE userid = ?";
		Connection conn = bd1.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				User user = new User();
				user.setUserid(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setSex(rs.getInt(4));
				user.setPhonenum(rs.getString(5));
				user.setStatus(rs.getInt(6));
				user.setSigndate(rs.getDate(8));
				user.setBrithday(rs.getDate(7));
				user.setLike(rs.getInt(9));
				user.setFavorableRate(rs.getDouble(10));
				user.setAddress(rs.getString(11));
				user.setFannum(rs.getInt(12));
				user.setFollownum(rs.getInt(13));
				user.setBuyertag(rs.getInt(14));
				user.setSellernum(rs.getInt(15));
				user.setIntroduce(rs.getString(16));
				user.setHeadimg(rs.getString(17));
				user.setSoldCount(rs.getInt(18));
				user.setReplyRate(rs.getDouble(19));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return null;
	}
	@Override
	public int sqlUpdateGoodsNum(Goods good,int i) {
		// TODO Auto-generated method stub
		String sql = "UPDATE `goods` SET `goodnum` = ? WHERE `goodid` = ?";
		Object[] o = {i,good.getGoodid()};
		return bd1.executeUpdate(sql, o);
	}

}
